<?php
//session_start ();
require_once 'ConstantContact.php';
require_once 'config.php';
	
	
// if you have a username stored in the Datastore (this is done inside config.php)
if (isset($DatastoreUser)) {
    //$ConstantContact = new ConstantContact("oauth2", $apiKey, $username, $consumerSecret);
    $ConstantContact = new ConstantContact("basic", $apiKey, $username, $password);

    // Add data to post request
    $fileContent = '';
    $fileName = 'subscribers.txt';
    $handle = @fopen(dirname(__FILE__) . '/' . $fileName, "r");
    if ($handle) {
        while (($buffer = fgets($handle)) !== false) {
            $fileContent .= $buffer;
        }

        fclose($handle);
    }

    // Prepare data
    $data = array(
        'activityType' 	=> 'SV_ADD',
        'data'			=> $fileContent,
    );
    $data = http_build_query($data);

    // Get lists
    $ContactLists = $ConstantContact->getLists();
    foreach ($ContactLists['lists'] as $k => $item) {
        $data .= '&lists=' . urlencode($item->id);
    }

    echo $data;
    $result = $ConstantContact->bulkAddContacts($data);
    echo $result;
}
else{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Newsletter - Add data to ConstantContact</title>
        <base href="http://havhokeren.server1881383095.internet-server.dk/admin/">
        <link rel="icon" href="view/image/favicon.png">
        <link href="view/stylesheet/stylesheet.css" type="text/css" rel="stylesheet">
    </head>
<body>
<div id="container">
    <div id="header">
        <div class="div1"><img onclick="location = 'http://havhokeren.server1881383095.internet-server.dk/admin/index.php?route=common/home&amp;token=d3d9446802a44259755d38e6d163e820'" title="Administration" src="view/image/logo.png"></div>
        <div class="div2"><img style="position: relative; top: 3px;" alt="" src="view/image/lock.png">&nbsp;You are logged in as <span>admin</span></div>
    </div>
    <div id="menu"></div>
    <div id="content">
        <div class="box">
            <div class="left"></div>
            <div class="right"></div>
            <div class="heading">
                <h1 style="background-image: url('view/image/mail.png');">Subscribers contact to ConstantContact</h1>
                <div class="buttons"></div>
            </div>
            <div class="content">
                <h1>ConstantContact authentication</h1>
                <h3>Click the link below to authorize your application to access your Constant Contact account resources.</h3>

                <?php
                // you must encode your redirect URL and the link contains the variables from config.php
                $theRequest = urlencode ( $verificationURL );
                echo "<a href='https://oauth2.constantcontact.com/oauth2/oauth/siteowner/authorize?response_type=code&client_id="
                    . $apiKey . "&redirect_uri="
                    . $theRequest
                    . '?file=subscribers'
                    . "'>Authorize here</a>";
                ?>
            </div>
        </div>
    </div>
</div>
<div id="footer">
    <div id="page_content">
        <span style="float:left; margin-left:50px;">Copyright &copy; 2012 havhokeren.dk. All rights reserved</span>
        <span style="float:right; margin-right:50px;">Design by<a target="_blank" href="http://www.mwc.vn"> My Web Creations</a></span>
        <p class="cb"></p>
    </div>
</div>
</body>
</html>
<?php } ?>
